﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;

namespace Zwms.Client.InfoSys
{
    public partial class SysLoginInfoQueryWnd : UserControl
    {
        #region 成员函数、属性
        private SysLoginInf _queryRecord;
        private PagerIndex _pagerIndex;
        public SysLoginInf QueryRecord { get { return _queryRecord; } }
        #endregion 

        #region 构造函数
        public SysLoginInfoQueryWnd()
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
        }
        private void InitControl()
        {
            ResetDataPager();
            ResetQueryRecord();
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            RefreshData();
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _queryRecord = new SysLoginInf();
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            LoadRecordList(this._queryRecord, ref this._pagerIndex);
        }

        private void GetRecordFromUI()
        {
            _queryRecord.UserCode = this.tbUserCode.Text.Trim();
            _queryRecord.UserName = this.tbUserName.Text.Trim();
            if (this.dpuSysDate.SelectDateTime != null)
            {
                _queryRecord.SysDate = this.dpuSysDate.SelectDateTime;
            }
            if (!string.IsNullOrWhiteSpace(tbsDepartment.GetTextBoxContent()))
            {
                _queryRecord.DepartmentCode = tbsDepartment.GetTextBoxContent();
            }
            if (this.cbLogSgn.SelectedIndex != -1)
            {
                string strLogSgn = CommonHelper.GetDictKeyByValue(CommonDefine.DictLogSgn,cbLogSgn.SelectedItem.ToString());
                if(strLogSgn!=null)
                {
                    _queryRecord.LogSgn = strLogSgn.Trim();
                }
            }
            
        }
        private void LoadRecordList(SysLoginInf record,ref PagerIndex pgIndex)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                SysLoginInf temp = new SysLoginInf();
               // record.UserCode = "112233";
               // record.SysDate = "20110101";
               // record.LogTime = "110201";
               // record.LogSgn = "1";
              //  temp = client.QueryLogInf(record);
                IList<SysLoginInf> recordList = client.QueryLogInfList(record, ref pgIndex);
                dataPager.RecordCount = pgIndex.RecordCount;
                this.sysLoginInfBindingSource.DataSource = null;
                this.sysLoginInfBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }
        #endregion

        #region 控件函数
        private void btnQuery_Click(object sender, EventArgs e)
        {
            ResetDataPager();
            ResetQueryRecord();
            GetRecordFromUI();
            this._pagerIndex.CurrentPageIndex = 1;
            LoadRecordList(this._queryRecord,ref _pagerIndex);
        }

        private void tbsDepartment_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.DepartmentWnd uc = new InfoWms.DepartmentWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择部门");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                this.tbsDepartment.SetTextBoxContent(uc.SelectedRecord.DepartmentName);
                this.tbsDepartment.Tag = uc.SelectedRecord.Id;
            }
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            ResetQueryRecord();
            GetRecordFromUI();
            LoadRecordList(this._queryRecord, ref _pagerIndex);
        }

    }
        #endregion
}
